home *** CD-ROM | disk | FTP | other *** search
- /* CPROG12.CPP - Set up and use a structure tag */
-
- #include <stdio.h>
- #include <string.h>
-
- //----------------------------------------------------------------
-
- struct person { int age;
- char name[30];
- };
-
- /* This doesn't create a structure - it just defines a template, called
- person, that may be used to make strcutures in this format. Person is a
- structure tag. We could, if we wanted to, create actual structures in
- this declaration by naming them between the closing brace and the ; */
-
- //----------------------------------------------------------------
- void afunction(void)
- {
- struct person doris;
-
- doris.age=73;
- strcpy( doris.name, "Doris Smith");
- printf("Name: %s Age: %d\n", doris.name, doris.age);
- }
-
- /* Doris is a structure of type person. Since she is volatile, her components
- must be initialised with explicit program instructions. */
-
- //----------------------------------------------------------------
-
- void main(void)
- {
- static struct person fred = {30, "Fred Jones"};
-
- afunction();
- printf("Name: %s Age: %d\n", fred.name, fred.age);
- }
-
- /* Fred is another strcuture of type person. Because he is static, he can
- be initialised during compilation. */
-